package com.ymatou.userregister.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userregister.parameter.BindEmailPhoneBean;
import com.ymatou.userregister.service.BindPhoneCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

public class Ts_BindPhone {
	private static BindEmailPhoneBean bindPhoneBean;
    private static BindPhoneCall bindPhoneCall;
    private ULC201406Wapper userWapper;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定用户登陆手机");
	}

	@Before
	public void caseUp() throws Exception {
		bindPhoneBean = new BindEmailPhoneBean();
		bindPhoneCall = new BindPhoneCall();
		userWapper = new ULC201406Wapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindPhoneCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindPhone_001() {
		Logger.start(true,"绑定手机成功");
		try {
			String phone = TestDataManager.getPhoneNum();
			Map user = UserCallService.registerUser(TestDataManager.getRandomNum(7)+"@RegTest.com", phone,null);
			
			bindPhoneBean.setAction("BindMobile");
			bindPhoneBean.setActionSource("1");
			bindPhoneBean.setLoginId(user.get("loginId").toString());
			bindPhoneBean.setLoginPassword("123456a");
			bindPhoneBean.setMobile(phone);
			bindPhoneBean.setUserId((Integer)user.get("userId"));
			bindPhoneCall.setData(bindPhoneBean);
			bindPhoneCall.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, bindPhoneCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", bindPhoneCall.getString("Success"), "验证Success");
			Logger.verifyEquals("14", bindPhoneCall.getString("Result"), "验证凭据（14表示操作成功）");
			ULC201406Bean users = userWapper.getULCInfoByUid((Integer)user.get("userId"));
			Logger.verifyEquals(phone, users.getlBills().getMobile(), "验证电话号码信息");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindPhone_002() {
		Logger.start(true,"修改登陆手机成功");
		try {
			String phone = TestDataManager.getPhoneNum();
			Map user = UserCallService.registerUser(TestDataManager.getRandomNum(7)+"@RegTest.com", phone,null);
			String newPhone = TestDataManager.getPhoneNum();
			
			bindPhoneBean.setAction("ModifyLoginMobile");
			bindPhoneBean.setActionSource("1");
			bindPhoneBean.setLoginId(user.get("loginId").toString());
			bindPhoneBean.setLoginPassword("123456");
			bindPhoneBean.setMobile(newPhone);
			bindPhoneBean.setUserId((Integer)user.get("userId"));
			bindPhoneCall.setData(bindPhoneBean);
			bindPhoneCall.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, bindPhoneCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", bindPhoneCall.getString("Success"), "验证Success");
			Logger.verifyEquals("14", bindPhoneCall.getString("Result"), "验证凭据（14表示操作成功）");
			ULC201406Bean users = userWapper.getULCInfoByUid((Integer)user.get("userId"));
			Logger.verifyEquals(newPhone, users.getlBills().getMobile(), "验证电话号码信息");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindPhone_003() {
		Logger.start(false,"不输入操作，失败");
		try {
			String phone = TestDataManager.getPhoneNum();
			Map user = UserCallService.registerUser(TestDataManager.getRandomNum(7)+"@RegTest.com", null,null);
			
			bindPhoneBean.setActionSource("1");
			bindPhoneBean.setLoginId(user.get("loginId").toString());
			bindPhoneBean.setLoginPassword("123456");
			bindPhoneBean.setMobile(phone);
			bindPhoneBean.setUserId((Integer)user.get("userId"));
			bindPhoneCall.setData(bindPhoneBean);
			bindPhoneCall.callService();
			
			Logger.verifyEquals(ECode.ERROR400, bindPhoneCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", bindPhoneCall.getString("Success"), "验证Success");
			Logger.verifyEquals("dto验证失败", bindPhoneCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("20", bindPhoneCall.getString("Result"), "验证凭据（20表示错误的DTO数据结构）");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindPhone_004() {
		Logger.start(false,"绑定已绑定过的电话号码，失败");
		try {
			ULC201406Bean existUser = userWapper.getULCInfoByUid(100);
			Map user = UserCallService.registerUser(TestDataManager.getRandomNum(7)+"@RegTest.com", null,null);
			
			bindPhoneBean.setAction("BindMobile");
			bindPhoneBean.setActionSource("1");
			bindPhoneBean.setLoginId(user.get("loginId").toString());
			bindPhoneBean.setLoginPassword("123456");
			bindPhoneBean.setMobile(existUser.getlBills().getMobile());
			bindPhoneBean.setUserId((Integer)user.get("userId"));
			bindPhoneCall.setData(bindPhoneBean);
			bindPhoneCall.callService();
			
			Logger.verifyEquals(ECode.ERROR400, bindPhoneCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", bindPhoneCall.getString("Success"), "验证Success");
			Logger.verifyEquals("手机已被其他用户使用", bindPhoneCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("26", bindPhoneCall.getString("Result"), "验证凭据（26表示手机已被其他用户使用）");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindPhone_005() {
		Logger.start(false,"不输入要绑定的电话号码，失败");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getRandomNum(7)+"@RegTest.com", null,null);
			bindPhoneBean.setAction("BindMobile");
			bindPhoneBean.setActionSource("1");
			bindPhoneBean.setLoginId(user.get("loginId").toString());
			bindPhoneBean.setLoginPassword("123456");
			bindPhoneBean.setUserId((Integer)user.get("userId"));
			bindPhoneCall.setData(bindPhoneBean);
			bindPhoneCall.callService();

			Logger.verifyEquals(ECode.ERROR400, bindPhoneCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", bindPhoneCall.getString("Success"), "验证Success");
			Logger.verifyEquals("dto验证失败", bindPhoneCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("2", bindPhoneCall.getString("Result"), "验证凭据（2表示手机号码格式错误）");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindPhone_006() {
		Logger.start(false,"输入错误格式的电话号码，失败");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getRandomNum(7)+"@RegTest.com", null,null);
			bindPhoneBean.setAction("BindMobile");
			bindPhoneBean.setActionSource("1");
			bindPhoneBean.setLoginId(user.get("loginId").toString());
			bindPhoneBean.setLoginPassword("123456");
			bindPhoneBean.setUserId((Integer)user.get("userId"));
			bindPhoneBean.setMobile(TestDataManager.getRandomNum(5));
			bindPhoneCall.setData(bindPhoneBean);
			bindPhoneCall.callService();

			Logger.verifyEquals(ECode.ERROR400, bindPhoneCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", bindPhoneCall.getString("Success"), "验证Success");
			Logger.verifyEquals("手机格式错误", bindPhoneCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("2", bindPhoneCall.getString("Result"), "验证凭据（2表示手机号码格式错误）");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}
